// ----------------------------------------------------------------------------
// Copyright 2006-2008, GeoTelematic Solutions, Inc.
// All rights reserved
// ----------------------------------------------------------------------------
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ----------------------------------------------------------------------------
// Change History:
//  2008/02/21  Martin D. Flynn
//     -Moved from JavaScriptTools.java
// ----------------------------------------------------------------------------

// --- String.trim(...)

String.prototype.trim = function() { return this.replace(/^\\s+/,'').replace(/\\s+$/,''); }

// ----------------------------------------------------------------------------
// --- Number parsing/formatting

/* encode characters */
function strEncode(s)
{
    return encodeURIComponent(s);   // escape(s);
}

/* encode characters */
function strDecode(s)
{
    return decodeURIComponent(s);   // unescape(s);
}

// ----------------------------------------------------------------------------
// --- Number parsing/formatting

/* parse numeric value */
function numParseFloat(val, dft) 
{
   var num = parseFloat(val);
   if (isNaN(num)) { num = dft; }
   return num;
}

/* format floating-point value with specified number of decimal points*/
// an unsophisticated numeric formatter
function numFormatFloat(val, dec) 
{
   var num = numParseFloat(val,0);
   if (dec > 0) {
      var neg = (num >= 0)? '' : '-';
      num = Math.abs(num);
      var d;
      for (d = 0; d < dec; d++) { num *= 10; }
      num = parseInt(num + 0.5);
      var str = new String(num);
      while (str.length <= dec) { str = '0' + str; }
      str = str.substring(0, str.length - dec) + '.' + str.substring(str.length - dec);
      return neg + str;
   } else {
      num = parseInt((num >= 0)? (num + 0.5) : (num - 0.5));
      return new String(num);
   }
}

// ----------------------------------------------------------------------------
// --- latitude/logiutude distance calculations

var EARTH_RADIUS_KM     = 6371.0088;
var EARTH_RADIUS_METERS = EARTH_RADIUS_KM * 1000.0;

/* Square */
function geoSQ(val) 
{
   return val * val;
}

/* degrees to radians */
function geoRadians(deg) 
{
   return (deg * Math.PI) / 180.0;
}

/* radians to degrees */
function geoDegrees(rad) 
{
   return (rad * 180.0) / Math.PI;
}

/* return distance (in radians) between points */
function geoDistanceRadians(lat1, lon1, lat2, lon2) 
{
   var rlat1 = geoRadians(lat1);
   var rlon1 = geoRadians(lon1);
   var rlat2 = geoRadians(lat2);
   var rlon2 = geoRadians(lon2);
   var dtlat = rlat2 - rlat1;
   var dtlon = rlon2 - rlon1;
   var a     = geoSQ(Math.sin(dtlat/2.0)) + (Math.cos(rlat1) * Math.cos(rlat2) * geoSQ(Math.sin(dtlon/2.0)));
   var rad   = 2.0 * Math.atan2(Math.sqrt(a), Math.sqrt(1.0 - a));
   return rad;
}

/* return distance (in meters) between points */
function geoDistanceMeters(lat1, lon1, lat2, lon2) 
{
   var meters = geoDistanceRadians(lat1,lon1,lat2,lon2) * EARTH_RADIUS_METERS;
   return meters;
}

// ----------------------------------------------------------------------------
// --- cookie utilities

var cookieTag = 'OpenGTS';

/* set cookie */
function setCookie(key, val) 
{
  var d;
  if (val == null) {
    d = (new Date(0)).toGMTString();
    val = '';
  } else {
    var expireMin = 15;
    d = new Date((new Date()).getTime() + (expireMin * 60000));
  }
  document.cookie = cookieTag+ '.' + key + '=' + strEncode(val) + ';; expires=' + d;
}

/* get cookie */
function getCookie(key, dft) 
{
  var c = document.cookie, k = cookieTag+ '.' + key + '=', p = c.indexOf(k);
  if (p >= 0) {
    var pe = c.indexOf(';',p);
    if (pe < 0) { pe = c.length; }
    return strDecode(c.substring(p + k.length, pe));
  } else {
    return dft;
  }
}

// ----------------------------------------------------------------------------
// --- Parse request URL

/* return argument for specified key in request string */
function getQueryArg(argName) 
{
  var mainURL = window.location.search;
  var argStr = mainURL.split('?');
  if (argStr.length > 1) {
    var args = argStr[1].split('&');
    for (i in args) {
      var keyVal = args[i].split('=');
      if (argName == keyVal[0]) {
        //document.write('Found Key: ' + keyVal[0] + ' == ' + keyVal[1] + '<br>');
        return keyVal[1];
      }
    }
  }
  return null;
}

// ----------------------------------------------------------------------------
// --- Element location/size

/* return relative position of specified element */
function getElementPosition(elem) 
{
  var ofsLeft = 0;
  var ofsTop  = 0;
  var ofsElem = elem;
  while (ofsElem) {
    ofsLeft += ofsElem.offsetLeft; // - ofsElem.scrollLeft;
    ofsTop  += ofsElem.offsetTop;  // - ofsElem.scrollTop;
    ofsElem  = ofsElem.offsetParent;
  }
  if ((navigator.userAgent.indexOf('Mac') >= 0) && (typeof document.body.leftMargin != 'undefined')) {
    ofsLeft += document.body.leftMargin;
    ofsTop  += document.body.topMargin;
  }
  return { left:ofsLeft, top:ofsTop };
}

/* return size of specified element */
function getElementSize(elem) 
{
  return { width:elem.offsetWidth, height:elem.offsetHeight };
}

// ----------------------------------------------------------------------------
// --- Element location/size

/* open a resizable window and display specified URL */
function openResizableWindow(url, name, W, H) 
{
  var attr = 'resizable=yes,width=' + W + ',height=' + H;
  var L = ((screen.width - W) / 2), T = ((screen.height - H) / 2);
  attr += ',screenX=' + L + ',screenY=' + T + ',left=' + L + ',top='+T;
  var win = window.open(url, name, attr, false);
  win.moveTo(L,T);
  win.focus();
}

// ---
